#include "ascii_art.hpp"

AsciiArt::AsciiArt(){
    /* Load a pixmap from the specified file. */
    //QPixmap pixmap;
    //pixmap.load(index.model()->data(index, Qt::DisplayRole).toString());

    ///* Scale pixmap to appropriate size. */
    //QPixmap scaled = pixmap.scaled(maxWidth->value(), maxHeight->value(), Qt::KeepAspectRatio);
    //picture->setPixmap(scaled);
    //picture->setAlignment(Qt::AlignHCenter | Qt::AlignTop);

    ///* Get a QImage from the pixmap, this allows us to access the pixels. */
    //QImage image;
    //image = scaled.toImage();

    ///* Create our worker thread object. */
    //worker = new WorkerThread;
    //worker->start();
    //worker->moveToThread(worker);

    ///* Connect our outbound signal to the worker thread's inbound slot. */
    //connect(this, SIGNAL(beginWork(QImage)), worker, SLOT(beginWork(QImage)));

    ///* Connect our inbound slots to the worker thread's signals. */
    //connect(worker, SIGNAL(progress(int)), opProgress, SLOT(setValue(int)));
    //connect(worker, SIGNAL(work_finished(QStringList)), this, SLOT(work_finished(QStringList)));

    ///* Start work in the worker thread. */
    //emit beginWork(image);
}

AsciiArt::~AsciiArt(){
    delete worker;
}

/* odkomentujeme az budeme vedet co to bude az to bude, ale asi nejaky plain text edit - musim dohodnut s timom
void AsciiArt::work_finished(QStringList output)
{
    //if (radioHtml->isChecked())       code->setPlainText(output.at(0));
    //else if (radioPhpBB->isChecked()) code->setPlainText(output.at(1));
    //else if (radioVb->isChecked())    code->setPlainText(output.at(2));

    //code->selectAll();
    //preview->setHtml(output.at(0));
    //opProgress->setValue(100);
    worker->wait();
    delete worker;
}*/

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/* Create the ascii art in all three supported formats, html
   at 0, phpbb at 1 and vbulletin at 2. */
QStringList WorkerThread::createAsciiArt(const QImage& image)
{
    QStringList output;

    output.push_back("<html><body>");                  /* HTML */
    output.push_back("[size=7][color=#FFFFFF]");       /* PHPBB */
    output.push_back("[code][size=1][color=#FFFFFF]"); /* VBulletin */

    for (int i = 0; i < image.height(); i++)
    {
        for (int j = 0; j < image.width(); j++)
        {
            if (j == 0 || image.pixel(j - 1, i) != image.pixel(j, i))
            {
                /* Color has changed or we're on a new row. Output a new font tag. */
                if (j != 0)
                {
                    output[0] += "</font>"; output[1] += "[/color]"; output[2] += "[/color]";
                }

                output[0] += "<font color=\"#"; output[1] += "[color=#"; output[2] += "[color=#";

                QString colorCode;
                QRgb rgb = image.pixel(j, i);
                colorCode += QString("%1").arg(qRed(rgb),   2, 16, QChar('0'));
                colorCode += QString("%1").arg(qGreen(rgb), 2, 16, QChar('0'));
                colorCode += QString("%1").arg(qBlue(rgb),  2, 16, QChar('0'));

                output[0] += colorCode; output[1] += colorCode; output[2] += colorCode;
                output[0] += "\">"; output[1] += "]"; output[2] += "]";
            }
            output[0] += "x"; output[1] += "x"; output[2] += "x";
        }

        /* Emit progress at the end of every line. */
        emit progress(i * 100 / image.height());
        output[0] += "</font>"; output[1] += "[/color]"; output[2] += "[/color]";
        output[0] += "<br />";
    }

    output[0] += "</body></html>"; output[1] += "[/color][/size]"; output[2] += "[/color][/size][/code]";
    return output;
}


void WorkerThread::run()
{
    /* Start the event loop. */
    exec();
}

void WorkerThread::beginWork(QImage image)
{
        /* Convert to 32 bit format if required. */
        if (image.format() != QImage::Format_RGB32)
        {
            image = image.convertToFormat(QImage::Format_RGB32);
        }

        /* Now convert the image into ascii art! */
        QStringList output = createAsciiArt(image);

        /* Tell the master we're finished and exit our worker thread. */
        emit work_finished(output);
        exit(0);
}
